package com.srwl.mytx.task;

import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;

import com.alibaba.fastjson.JSONObject;
import com.amap.api.location.AMapLocation;
import com.srwl.mytx.Constant;
import com.srwl.mytx.domain.Contact;
import com.srwl.mytx.domain.LatLng;
import com.srwl.mytx.domain.Message;
import com.srwl.mytx.domain.User;
import com.srwl.mytx.im.MessageProcessor;
import com.srwl.mytx.map.GaodeLocationClient;
import com.srwl.mytx.parse.UserProfileManager;
import com.srwl.mytx.utils.IDUtils;

import java.util.Date;

/**
 * Created by dahai2070 on 2018/4/18 0018.
 */

public class RequestLocationTask extends AsyncTask<Object, Void, Void> {
    private static String TAG = RequestLocationTask.class.getSimpleName();
    private GaodeLocationClient location;
    private Contact contact;
    private LocationListener locationListener;
    private Context context;

    public RequestLocationTask(Contact contact, Context context) {
        this.contact = contact;
        this.context = context;
    }

    @Override
    protected Void doInBackground(Object[] objects) {

        location = new GaodeLocationClient((Context) objects[0],true);
        location.setLocationListener(new GaodeLocationClient.LocationListener() {
            @Override
            public void locationSuccess(AMapLocation location) {
                //标记为透传消息，
                LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
                String address = location.getAddress();
                Log.d(TAG, address);

                User loginUserInfo = UserProfileManager.getInstance().getLoginUserInfo();


                User user = new User();
                String targetUserId = contact.gettId();
                user.setuId(loginUserInfo.getuId());
                user.setLatLng(latLng);
                user.setArea(address);
                user.setAvatar(loginUserInfo.getAvatar());
                user.setUsername(loginUserInfo.getUsername());
                user.setCreated(new Date());

                Message notify = new Message();
                notify.setId(IDUtils.genOrderItemId());
                notify.setFromId(loginUserInfo.getuId());
                notify.setToId(targetUserId);
                notify.setType(Constant.NOTIFY_MESSAGE_CONTACT);//这个分类，是为了让接收方会把消息分发到contactNotifyConversation 处理
                notify.setSubType(Constant.PUSH_MESSAGE_LOCATION_RESULT);
                notify.setExtraData(JSONObject.toJSONString(user));
                notify.setVisible(Constant.MESSAGE_INVISIBLE);
                MessageProcessor.getInstance().sendMsg(notify, Constant.NOTIFY_MESSAGE);// 主意，消息分类是用的 NOTIFY_MESSAGE，而不是用的 C2C_MESSAGE，
                Log.i(TAG, "发出位置信息" + contact.getcId());

                if (locationListener != null) {
                    locationListener.onLocationSuccess(contact.getcId());
                }

            }
        });

        return null;
    }

    public void setLocationListener(LocationListener locationListener) {
        this.locationListener = locationListener;
    }

    public interface LocationListener {
        void onLocationSuccess(String contactId);
    }


}
